
$(function () {
    $('#recordTb').datagrid({
        rownumbers: true,//显示行号
        striped: true,
        fit: true,
        singleSelect: true,
        sortOrder: true,
        fitColumns: true,
        pagination: true,
        toolbar: '#toolbar',
        url: ROOT_PATH + '/train/record/MyReTable.shtml',
        queryParams: {
            sort: 'addTime',
            order: 'desc'
        },
        columns: [[
            {
                field: 'id',checkbox:true
            },
            {field: 'year', title: '年度', width: 80},
            {field: 'name', title: '培训班名称', width: 150,formatter:function (value, row, index) {
                return "<a href='javascript:void(0)' class='easyui-linkbutton text-primary' plain='true'   onclick='infoRecord(\""+row.recordId+"\",\""+row.planId+"\");'>"+row.name+"</a>";
            }},
            {field: 'realName', title: '姓名', width: 100},
            {field: 'code', title: '编号', width: 100},
            {field: 'isQualified', title: '是否合格', width: 100,
                formatter: function (value, row, index){
                    return row.isQualified=='1'?'是':(row.isQualified=='0'?'否':'');
                }},
            {field: 'score', title: '分数', width: 130},
            {field: 'trainContent', title: '培训内容', width: 300},
            {field: 'trainTeacher', title: '培训教师', width: 100},
            {field: 'actualStartTime', title: '开班时间', width: 100},
            {field: 'actualTrainHour', title: '培训学时', width: 100},
        ]]
    });
    
    $('#btn_advanced_search').on("click", function () {
        $('#queryForm2').toggle();
        $('#recordTb').datagrid('resize');
    });
    $('#btn_clear').on("click", function () {
        $('#queryForm1').form('reset');
        $('#queryForm2').form('reset');
    });
    var TRAINFORM=CONSTANT.DictionaryTypeDefConstants.TRAINFORM;//培训形式
    queryComboData(TRAINFORM,'trainFormId');
    var TRAIN_TRAINTYPE=CONSTANT.DictionaryTypeDefConstants.TRAIN_TRAINTYPE;//培训类别标识
    queryComboData(TRAIN_TRAINTYPE,'trainTypeId');
    var TRAIN_PROTYPE=CONSTANT.DictionaryTypeDefConstants.TRAIN_PROTYPE;//专业类别标识
    queryComboData(TRAIN_PROTYPE,'professionalTypeId');
    var TRAIN_LEVEL=CONSTANT.DictionaryTypeDefConstants.TRAIN_LEVEL;//培训级别标识
    queryComboData(TRAIN_LEVEL,'trainLevelId');

    queryYearthList("yearAnnual");
});


function queryComboData(code,queryId) {
    var EMPTY_OPTION = {
        id: '',
        text: '请选择'
    };
    var pageId='#'+queryId;
    $.ajax({
        type: 'post',
        url: ROOT_PATH + '/permission/dictionary/queryDictionaryByDictionaryTypeCode.shtml',
        data: {code: code},
        dataType: "json",
        success: function (result) {
            $(pageId).combobox({
                loadFilter: function (data) {
                    data.unshift(EMPTY_OPTION);
                    return data;
                },
                data: result,
                prompt:'请选择',
                valueField: 'id',
                textField: 'text',
                editable: false,
                panelHeight: '200',
                onSelect: function (record) {
                }
            });
        }
    });
}
function planStartMonth(id) {
    $(id).datebox({
        onShowPanel: function () {//显示日趋选择对象后再触发弹出月份层的事件，初始化时没有生成月份层
            span.trigger('click'); //触发click事件弹出月份层
            if (!tds) setTimeout(function () {//延时触发获取月份对象，因为上面的事件触发和对象生成有时间间隔
                tds = p.find('div.calendar-menu-month-inner td');
                tds.click(function (e) {
                    e.stopPropagation(); //禁止冒泡执行easyui给月份绑定的事件
                    var year = /\d{4}/.exec(span.html())[0]//得到年份
                        , month = parseInt($(this).attr('abbr'), 10); //月份，这里不需要+1
                    $('#planStartMonth').datebox('hidePanel')//隐藏日期对象
                        .datebox('setValue', year + '-' + month); //设置日期的值
                });
            }, 0);
            yearIpt.unbind();//解绑年份输入框中任何事件
        },
        parser: function (s) {
            if (!s) return new Date();
            var arr = s.split('-');
            return new Date(parseInt(arr[0], 10), parseInt(arr[1], 10) - 1, 1);
        },
        formatter: function (d) {
            var a = parseInt(d.getMonth())<parseInt('9')?'0'+parseInt(d.getMonth()+ 1):d.getMonth() + 1;
            return d.getFullYear() + '-' +a; }
    });
    var p = $(id).datebox('panel'), //日期选择对象
        tds = false, //日期选择对象中月份
        yearIpt = p.find('input.calendar-menu-year'),//年份输入框
        span = p.find('span.calendar-text'); //显示月份层的触发控件
}
/**
 * 重新加载表格
 */
function reLoadData() {
    $('#recordTb').datagrid('clearSelections');
    var queryParams1 = $('#queryForm1').serializeJson();
    var queryParams2 = $('#queryForm2').serializeJson();
    var queryParams = $.extend(queryParams1,queryParams2);
    queryParams["sort"] = 'addTime';
    queryParams["order"] = 'desc';
    $('#recordTb').datagrid('options').queryParams = queryParams;
    $("#recordTb").datagrid('reload', ROOT_PATH + '/train/record/MyReTable.shtml');
}
function queryYearthList(id){
    var date = new Date();
    var year = date.getFullYear();
    var yearthData = new Array();
    yearthData.push({id:"",text:"请选择"});
    for(var i =0;i<6;i++){
        var y = {id:year-i,text:(year-i)+'年'}
        yearthData.push(y);
    }
    $('#'+id).combobox(
        {
            data:yearthData,
            valueField:'id',
            textField:'text'
        }
    );
}
/**
 * 打开弹框
 * @param title
 * @param content
 */
function openLayers(title, content) {
    WindowManager.openLayer({
        type: 2,
        resize: false,
        title: title,
        area: ['1150px', '80%'],
        content: content,
        callBack:function(data){
            if(data){
                reLoadData();
            }
        }
    });
}
function infoRecord(id,planId) {
    var title = '培训记录详情';
    var content = ROOT_PATH + '/train/record/info.shtml?resourceId=' + $.getQueryParam('resourceId')+'&id=' + id+'&planId=' + planId+'&type=info';
    openLayers(title, content);
}


